package com.yoomiito.app.ui.order.list


import android.text.TextUtils
import androidx.constraintlayout.widget.ConstraintLayout
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.TextView

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.model.bean.OrderInfo
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.SpannableUtils


class OrderAdapter(data: List<OrderInfo>?, private val mGoodsType: Int, private val type: Int) : BaseQuickAdapter<OrderInfo, BaseViewHolder>(R.layout.item_goods_order, data) {


    var onClick: OnClick? = null
    override fun convert(helper: BaseViewHolder, item: OrderInfo) {
        val goodsTypeIv = helper.getView<ImageView>(R.id.item_goods_order_type)
        val orderFlagIv = helper.getView<ImageView>(R.id.item_order_flag)
        val constraintLayout = helper.getView<ConstraintLayout>(R.id.item_order)
        //订单图标
        val orderIdTv = helper.getView<TextView>(R.id.item_goods_order_id)
//        if (mGoodsType == 1) {//淘宝
//            goodsTypeIv.visibility = View.VISIBLE
//            //            if (1 == item.getOrder_type()){
//            //                goodsTypeIv.setImageResource(R.mipmap.tb);
//            //            }else {
//            //                goodsTypeIv.setImageResource(R.mipmap.tmall);
//            //            }
//            //天猫
//            //天猫国际
//            //淘宝
//            //聚划算
//            //航旅
//            when(item.order_source) {
//                "淘宝" -> goodsTypeIv.setImageResource(R.mipmap.tb)
//                "天猫" -> goodsTypeIv.setImageResource(R.mipmap.tmall)
//                "聚划算" -> goodsTypeIv.setImageResource(R.mipmap.juhuasuan)
//                "航旅" -> goodsTypeIv.setImageResource(R.mipmap.feizhu)
//                "天猫国际" -> goodsTypeIv.setImageResource(R.mipmap.tmall_guoji)
//            }
//            orderIdTv.text = "订单号：" + item.trade_parent_id
//        } else {
//
//        }


        orderIdTv.text = "订单号：" + item.id
        val goodsSize = helper.getView<TextView>(R.id.item_goods_order_size)
        if (type == 3 || type == 4 ||  type == 5 ) {
            goodsSize.text = "共" + item.num + "件 实付金额: "
        } else {
            goodsSize.text = "实付金额: "
        }

        //商品图
        val goodsPicIv = helper.getView<ImageView>(R.id.item_goods_order_pic)
        ImageLoader.getInstance().into(goodsPicIv.context, item.item_picture, goodsPicIv, R.drawable.goods_default)


        if (item.status == 1) {

        } else {


        }
        //商品title
        val goodsTitleTv = helper.getView<TextView>(R.id.item_goods_order_title)

        if (type == 1 || type == 2) {
            goodsTitleTv.text = "用户姓名：" + item.userName
            if (TextUtils.isEmpty(item.userName)) {
                goodsTitleTv.visibility = View.GONE
            } else {
                goodsTitleTv.visibility = View.VISIBLE
            }
        } else {
            goodsTitleTv.text = item.productName
        }
        val clOther = helper.getView<ConstraintLayout>(R.id.cl_other)

        if (item.status == 3 && mGoodsType == 2) {
            clOther.visibility = View.VISIBLE
        } else {
            clOther.visibility = View.GONE
        }

        val mobile = helper.getView<TextView>(R.id.item_mobile)
        mobile.text = "手机号码：" + item.mobile
        if (TextUtils.isEmpty(item.mobile)) {
            mobile.visibility = View.GONE
        } else {
            mobile.visibility = View.VISIBLE
        }
        //订单时间
        val orderDateTv = helper.getView<TextView>(R.id.item_goods_order_date)
        orderDateTv.text = item.ctime

        //订单价格
        val priceTv = helper.getView<TextView>(R.id.item_goods_order_price)
        priceTv.text = "¥" + item.money
        //订单状态
        val orderStatusTv = helper.getView<TextView>(R.id.item_goods_order_status)
        if (item.status == 1) {
            orderStatusTv.setTextColor(ResUtil.getColor(R.color.colorAccent));
        } else if (item.status == 4) {
            orderStatusTv.setTextColor(ResUtil.getColor(R.color.color_balk_999999));
        } else {
            orderStatusTv.setTextColor(ResUtil.getColor(R.color.color_orange));

        }
//        orderStatusTv.setTextColor(ResUtil.getColor(R.color.color_orange))
        var status = item.statusStr
        if (mGoodsType == AllCode.TaoBao && item.status == 0) status = ""//淘宝订单不存在待支付订单
        orderStatusTv.text = status
        if (type == 1 || type == 2) {
            if (item.status == 3 && type == 2) {
                helper.setText(R.id.tv_text, "申请级别: ")
                helper.setText(R.id.tv_num, item.roleStr + "")
            } else {
                helper.setText(R.id.tv_text, "复购数量: ")
                helper.setText(R.id.tv_num, item.num + "")
            }
            helper.setVisible(R.id.ll_show, true)
            if (!TextUtils.isEmpty(item.StockType))
                helper.setText(R.id.tv_type, "(" + item.StockType + ")")
            else {
                if (!TextUtils.isEmpty(item.roleStr))
                    helper.setText(R.id.tv_type, "(" + item.roleStr + ")")
            }

        } else {
            helper.setVisible(R.id.ll_show, false)
        }


        helper.setOnClickListener(R.id.tv_reject, object : OnClickListener {
            override fun onClick(p0: View?) {
                onClick?.OnChoose(1, item.id)
            }

        })
        helper.setOnClickListener(R.id.iv_accept, object : OnClickListener {
            override fun onClick(p0: View?) {
                onClick?.OnChoose(2, item.id)
            }

        })
        //奖券
        val couponView = helper.getView<View>(R.id.item_goods_order_coupon)
        val couponTv = helper.getView<TextView>(R.id.tv_bonus)
//        setCoupon(item.user_income, couponView, couponTv)
        //        if (TextUtils.isEmpty(item.getUser_income())||"0.00".equals(item.getUser_income())){

        //订单失效标记
        orderFlagIv.visibility = if (5 == item.status) View.VISIBLE else View.GONE
        val view = helper.getView<View>(R.id.item_goods_order_view)
        when (item.status) {
            5 -> {
                view.visibility = View.VISIBLE
                orderFlagIv.setImageResource(R.mipmap.failure)
                view.setBackgroundResource(R.drawable.bg_gray_radius_4)
//                couponView.visibility = View.GONE

                //        } else if (4 == item.getStatus()) {
                //            view.setVisibility(View.VISIBLE);
                //            view.setBackgroundResource(R.drawable.bg_gray_radius_4);
                //            orderFlagIv.setVisibility(View.VISIBLE);
                //            orderFlagIv.setImageResource(R.mipmap.already_settled);
                //            setCoupon(item.getUser_income(), couponView, couponTv);
            }
//            4 -> {
//                view.visibility = View.VISIBLE
//                view.setBackgroundResource(R.drawable.bg_gray_radius_4)
//                orderFlagIv.visibility = View.VISIBLE
//                orderFlagIv.setImageResource(R.mipmap.already_settled)
//                setCoupon(item.user_income, couponView, couponTv)
//            }
            else -> {
                view.visibility = View.GONE
            }
        }
    }

    //设置奖
    private fun setCoupon(coupon: String, view: View, couponTv: TextView) {
        if (SpannableUtils.isEmptyOr0(coupon)) {
            view.visibility = View.GONE
        } else {
            view.visibility = View.VISIBLE
            couponTv.text = "¥" + SpannableUtils.handlePrice(coupon)
        }
    }


    public fun setOnChoose(onClick: OnClick) {
        this.onClick = onClick
    }

    public interface OnClick {
        fun OnChoose(type: Int, id: String)
    }
}
